package com.jstyle.jclifexd.daoManager;


import android.text.TextUtils;

import com.jstyle.jclifexd.dao.LastActivityDataDao;
import com.jstyle.jclifexd.model.LastActivityData;
import com.jstyle.jclifexd.utils.DbManager;
import com.jstyle.jclifexd.utils.SharedPreferenceUtils;

import org.greenrobot.greendao.query.QueryBuilder;

/**
 * Created by Administrator on 2018/12/29.
 */

public class LastActivityDataDaoManager {
    private static final String TAG = "LastActivityDataDaoMana";
    public static LastActivityData getLastActivityData(){
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address)){
            LastActivityData lastActivityData=new LastActivityData();
            lastActivityData.setAddress(address);
            insertLastActivityData(lastActivityData);
            return lastActivityData;
        }
        QueryBuilder<LastActivityData> queryBuilder= DbManager.getInstance().getDaoSession().getLastActivityDataDao().queryBuilder().where(LastActivityDataDao.Properties.Address.eq(address));
        if(queryBuilder.list().size()==0){
            LastActivityData lastActivityData=new LastActivityData();
            insertLastActivityData(lastActivityData);
            return lastActivityData;
        }
        return queryBuilder.list().get(queryBuilder.list().size()-1);
    }
    public static void insertLastActivityData(LastActivityData lastActivityData){
        DbManager.getInstance().getDaoSession().getLastActivityDataDao().insertOrReplace(lastActivityData);
    }
}
